import sys

from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtWidgets import QApplication, QMainWindow

from mainApp import Ui_MainWindow
from linai_server import CheckingServer, Task
from datetime import datetime  # 导入时间模块


class mainapp(QMainWindow, Ui_MainWindow):
    checking_server = None
    client_index = 1

    def __init__(self, parent=None):
        super(mainapp, self).__init__(parent)
        self.setupUi(self)
        self.openButton.clicked.connect(self.open_button_clicked)
        self.prevButton.clicked.connect(self.prev_button_clicked)
        self.nextButton.clicked.connect(self.next_button_clicked)
        self.leftBtn.clicked.connect(lambda: self.on_button_clicked("文字_向左转动"))
        self.rigthBtn.clicked.connect(lambda: self.on_button_clicked("文字_向右转动"))
        self.okBtn.clicked.connect(lambda: self.on_button_clicked("文字_验证通过"))
        self.helpBtn.clicked.connect(lambda: self.on_button_clicked("按钮_问号"))
        self.checking_server = CheckingServer()
        self.checking_server.update_screen.connect(self.on_update_screen)
        self.checking_server.log_message.connect(self.on_log_textedit)
        self.checking_server.start()

    def prev_button_clicked(self):
        # self.checking_server.clients[self.client_index].update_screen.disconnect(self.on_update_screen)
        # self.checking_server.clients[self.client_index].log_message.disconnect(self.on_log_textedit)

        self.client_index = self.client_index - 1
        if self.client_index < 1:
            self.client_index = 1
        self.checking_server.clients[self.client_index].update_screen.connect(self.on_update_screen)
        self.checking_server.clients[self.client_index].log_message.connect(self.on_log_textedit)

    def next_button_clicked(self):
        # self.checking_server.clients[self.client_index].update_screen.disconnect(self.on_update_screen)
        # self.checking_server.clients[self.client_index].log_message.disconnect(self.on_log_textedit)

        self.client_index = self.client_index + 1
        if self.client_index > len(self.checking_server.clients):
            self.client_index = len(self.checking_server.clients)
        self.checking_server.clients[self.client_index].update_screen.connect(self.on_update_screen)
        self.checking_server.clients[self.client_index].log_message.connect(self.on_log_textedit)

    def on_log_textedit(self, log):
        time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
        self.logTextEdit.appendPlainText("%s %s" % (time, log))

    def on_button_clicked(self, cls_text):
        # self.server_thread.send_click(cls_text)
        self.checking_server.add_task(Task(task_type='click', client_id=1, x=496, y=496))

    def open_button_clicked(self):
        pass

    def on_update_screen(self, pixmap, num_pixmap, icon_pixmap, target_icon_pixmap):
        print("on_update_screen start")
        # 将 QImage 转换为 QPixmap 并显示在 QLabel 中
        self.imageLabel.setPixmap(pixmap)
        self.numLabel.setPixmap(num_pixmap)
        self.iconLabel.setPixmap(icon_pixmap)
        self.targetIconLabel.setPixmap(target_icon_pixmap)
        print("on_update_screen end")


if __name__ == "__main__":
    app = QApplication(sys.argv)

    mainWindow = mainapp()
    mainWindow.setWindowFlags(Qt.WindowMinimizeButtonHint | Qt.WindowCloseButtonHint)
    mainWindow.setFixedSize(mainWindow.width(), mainWindow.height())
    mainWindow.showNormal()
    mainWindow.activateWindow()
    # server = CheckingServer()
    # server.run()
    sys.exit(app.exec_())
